<template>
	<view class="new-users copy-data">
		<!-- 筛选条件和搜索框 -->
		<view class="searchBox">
			<view class="filterBox">
				<picker @change="onFilterChange" :value="filterIndex" :range="filterOptions" class="filterPicker">
					<view class="filterText">{{ filterOptions[filterIndex] }}</view>
				</picker>
			</view>
			<uni-search-bar ref="searchBar" radius="30" placeholder="请输入关键字" clearButton="auto" cancelButton="none"
				@confirm="onSearchConfirm" class="searchInput" />
		</view>

		<!-- 状态导航 -->
		<view class='nav'>
			<view class='item' :class='status == null ? "on" : ""' @click="statusClick(null)">
				<view>全部</view>
			</view>
			<view class='item' :class='status == "0" ? "on" : ""' @click="statusClick('0')">
				<view>待确认</view>
			</view>
			<view class='item' :class='status == "2" ? "on" : ""' @click="statusClick('2')">
				<view>已到店</view>
			</view>
			<view class='item' :class='status == "3" ? "on" : ""' @click="statusClick('3')">
				<view>取消预约</view>
			</view>
		</view>

		<!-- 预约记录列表 -->
		<view class="moneyBox" v-for="(item, index) in appointmentList" :key="index">
			<view class="topUser">
				<p>{{ item.nickName }}</p>
				<p>{{ getStatusText(item.status) }}</p>
			</view>
			<view class="bottomMoney">
				<p>手机号：{{ item.mobile }}</p>
				<p>预约时间：{{ item.appointmentDate }} {{ item.appointmentTime }}</p>
			</view>
			<view class="buttonBox" v-if="item.status=='0'">
				<view class="button01" @click="editAppointment(item.id, 2)">已到店</view>
				<view class="button02" @click="editAppointment(item.id, 3)">取消预约</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		getAppointment,
		postAppointment
	} from '@/api/business.js';

	export default {
		data() {
			return {
				userInfo: {},
				appointmentList: [],
				pageSize: 10,
				pageNum: 1,
				status: null, // 订单状态
				filterIndex: 0, // 筛选条件索引，默认为 nickName
				searchKeyword: null, // 搜索关键字
				filterOptions: ['按用户姓名', '按手机号', '按用户ID'], // 筛选条件选项
				statusTextMap: {
					'0': '待确认',
					'1': '已确认',
					'2': '已完成',
					'3': '已取消',
					'4': '待取消'
				},
			};
		},
		computed: {
			getStatusText() {
				return (status) => this.statusTextMap['' + status] || '未知状态';
			},
		},
		onLoad() {
			this.getUserDetail();
		},
		methods: {
			getUserDetail() {
				getUserInfo().then((res) => {
					this.userInfo = res.data;
					this.loadMoreAppointments();
				});
			},
			statusClick(status) {
				if (status == this.status) return;
				this.status = status;
				this.pageNum = 1;
				this.appointmentList = [];
				this.loadMoreAppointments();
			},
			editAppointment(id, status) {
				const data = {
					id: id,
					status: status,
				};
				postAppointment(data).then(res => {
					this.$util.Tips({
						title: res.msg
					});
					this.appointmentList = [];
					this.loadMoreAppointments();
				});
			},
			async loadMoreAppointments() {
				const data = {
					storeId: this.userInfo.msqStore.id,
					pageSize: this.pageSize,
					pageNum: this.pageNum,
					status: this.status === '' ? null : this.status,
					nickName: this.filterIndex == 0 ? this.searchKeyword : null,
					mobile: this.filterIndex == 1 ? this.searchKeyword : null,
					uid: this.filterIndex == 2 ? this.searchKeyword : null,
				};

				try {
					const res = await getAppointment(data);
					const newList = res.data.records || []; // 假设返回的数据结构有一个rows字段
					this.appointmentList = this.appointmentList.concat(newList);
					this.pageNum++;
				} catch (error) {
					console.error('获取预约记录失败:', error);
				}
			},
			onFilterChange(e) {
				this.filterIndex = e.detail.value;
				this.searchKeyword = '';
				this.$refs.searchBar.clear(); // 调用 uni-search-bar 的 clear 方法清空输入框
			},
			onSearchConfirm(e) {
				this.searchKeyword = e.value;
				this.pageNum = 1;
				this.appointmentList = [];
				this.loadMoreAppointments();
			}
		},
		onReachBottom() {
			this.loadMoreAppointments();
		}
	};
</script>
<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.searchBox {
		background-color: #fff;
		padding: 20rpx;
		display: flex;
		align-items: center;

		.filterBox {
			margin-right: 20rpx;
		}

		.filterPicker {
			width: 150rpx; // 设置筛选条件选择器的宽度
		}

		.searchInput {
			flex-grow: 1;
		}
	}

	.nav {
		width: 100%;
		height: 86rpx;
		border-radius: 14rpx;
		background-color: #fff;
		display: flex;
	}

	.nav .item {
		width: 25%;
		text-align: center;
		font-size: 26rpx;
		color: #282828;
		padding: 26rpx 0;
	}

	.nav .item.on {
		font-weight: bold;
		color: #1D85E1;
		border-bottom: 5rpx solid #1D85E1;
	}

	.nav .item .num {
		margin-top: 18rpx;
	}

	.moneyBox {
		display: flex;
		flex-direction: column;
		padding: 36rpx 43rpx;
		margin: 30rpx;
		border-radius: 20rpx;
		background: #fff;

		.topUser {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #999;
			padding-bottom: 10rpx;

			p {
				margin: 0;
			}
		}

		.bottomMoney {
			width: 100%;
			color: #666666;
			padding-top: 10rpx;

			p {
				margin: 10rpx 0;
			}
		}

		.buttonBox {
			display: flex;
			justify-content: center;
			text-align: center;
			margin: 10rpx 0;

			.button01 {
				width: 290rpx;
				height: 54rpx;
				background: #1D85E1;
				border-radius: 27rpx;
				font-weight: 400;
				font-size: 26rpx;
				color: #FFFEFE;
				line-height: 54rpx;
			}

			.button02 {
				margin-left: 10rpx;
				width: 290rpx;
				height: 54rpx;
				background: #FFFFFF;
				border-radius: 27rpx;
				border: 1px solid #1D85E1;
				font-weight: 400;
				font-size: 26rpx;
				color: #1D85E1;
				line-height: 54rpx;
			}
		}
	}

	.loading {
		text-align: center;
		color: #999;
		padding: 20rpx;
	}

	.no-more {
		text-align: center;
		color: #ccc;
		padding: 20rpx;
	}
</style>